package view;

import utils.Tools;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;



public class RandomGroupView {
    private JFrame frame;
    private JLabel resultLabel;        // 结果显示标签
    private JCheckBox noRepeatCheck;   // 不重复抽取选项
    private List<String> allGroup;   // 所有班级
    private List<String> leftGroup;  // 未抽取的班级
    private Random random;
    private final int WIDTH = 450;    // 加宽窗口以便显示列表
    private final int HEIGHT = 350;

    public RandomGroupView() {
        frame = new JFrame("随机抽取小组");
        allGroup = new ArrayList<>();
        leftGroup = new ArrayList<>();
        random = new Random();

        // 加载小组数据
        loadGroup();
        // 初始化界面
        initView();

        Tools.setPos(frame,WIDTH,HEIGHT);
        frame.setSize(WIDTH, HEIGHT);  // 设置窗口大小
        frame.setLayout(null);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }

    private void initView() {
        // 1. 结果显示标签
        resultLabel = new JLabel("", JLabel.CENTER);
        resultLabel.setFont(new Font("宋体", Font.BOLD, 25));
        resultLabel.setBounds(50, 40, 300, 60);
        //设置这个 标签的边框
        resultLabel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        frame.add(resultLabel);

        // 2. 不重复抽取选项
        noRepeatCheck = new JCheckBox("不重复抽取");
        noRepeatCheck.setBounds(150, 130, 100, 30);
        //复选框的监听器
        noRepeatCheck.addActionListener(e -> {
            if (noRepeatCheck.isSelected()) {
                //重新设置抽取列表
                leftGroup = new ArrayList<>(allGroup);
            }
        });
        frame.add(noRepeatCheck);

        // 3. 抽取按钮
        JButton startBtn = new JButton("开始抽取");
        startBtn.setBounds(100, 180, 90, 30);
        startBtn.addActionListener(e -> randomSelect());
        frame.add(startBtn);

        // 4. 重置按钮
        JButton resetBtn = new JButton("重置");
        resetBtn.setBounds(210, 180, 90, 30);
        resetBtn.addActionListener(e -> reset());
        frame.add(resetBtn);

        // 5. 返回按钮
        JButton backBtn = new JButton("返回");
        backBtn.setBounds(155, 220, 90, 30);
        backBtn.addActionListener(e -> {
            frame.dispose();
            new MainView();
        });
        frame.add(backBtn);
    }

    // 加载小组数据
    private void loadGroup() {
        try {
            BufferedReader br = new BufferedReader(new FileReader("resources/Group.txt"));
            String line;
            while ((line = br.readLine()) != null) {
                if (!line.trim().isEmpty()) {
                    //加到全小组列表里面
                    allGroup.add(line.trim());
                }
            }
            br.close();
            //初始化未抽取列表
            leftGroup = new ArrayList<>(allGroup);

        } catch (IOException e) {
            JOptionPane.showMessageDialog(frame, "读取小组数据失败！");
        }
    }

    // 随机抽取
    private void randomSelect() {
        if (allGroup.isEmpty()) {
            JOptionPane.showMessageDialog(frame, "没有小组数据！");
            return;
        }

        if (noRepeatCheck.isSelected()) {
            // 不重复抽取模式
            if (leftGroup.isEmpty()) {
                JOptionPane.showMessageDialog(frame, "所有小组已抽取完！");
                return;
            }
            int index = random.nextInt(leftGroup.size());
            //移除已经抽取过的小组，显示
            resultLabel.setText(leftGroup.remove(index));
        } else {
            // 普通抽取模式
            int index = random.nextInt(allGroup.size());
            resultLabel.setText(allGroup.get(index));
        }
    }

    // 重置
    private void reset() {
        leftGroup = new ArrayList<>(allGroup);
        resultLabel.setText("点击开始抽取");
        JOptionPane.showMessageDialog(frame, "已重置！");
    }
}